<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2019/12/20
 * Time: 12:07
 */
namespace app\common\lib;
use think\App;
use util\TreeUtil;

class AdminMenu
{
    protected static $instance;
    protected $menu_list = [];
    protected $menu_tree = [];
    protected $app;
    protected $request;
    protected $breadcrumb = [];
    protected $uri;
    protected $current_menu;
    private function __construct($menu_list,$uri)
    {
        $this->app = \app();
        $this->request = $this->app->request;
        $this->uri = $uri;
        if(is_object($menu_list)){
            $menu_list = $menu_list->toArray();
        }
        $this->menu_list = $menu_list;
        $this->menu_tree = $this->initMenuTree();
        $this->current_menu = $this->initCurrentMenu();
        $this->initBreadCrumb(isset($this->current_menu['id']) ? $this->current_menu['id'] : 0);
    }
    private function __clone()
    {
    }
    public static function instance($menu_list,$uri){
        if(empty(self::$instance)){
            self::$instance = new static($menu_list,$uri);
        }
        return self::$instance;
    }
    public function getMenuTree(){
        return $this->menu_tree;
    }
    public function getCurrentMenu(){
        return $this->current_menu;
    }

    public function initMenuTree($is_hide = true){
        $menu_list = $this->menu_list;
        foreach ($menu_list as $key => $menu_info) {
            if (!empty($menu_info['is_hide']) && $is_hide) {
                unset($menu_list[$key]);
            }
        }
        return TreeUtil::instance()->list_to_tree(array_values($menu_list));
    }

    private function initCurrentMenu($path = null){
        if(empty($path)) {
            $path = $this->uri;
        }
        foreach ($this->menu_list as $menu){
            if(parse_name($menu['url']) == parse_name($path)){
                return $menu;
            }
        }
        return [];
    }

    public function getMenuInfo($menu_id){
        foreach ($this->menu_list as $menu){
            if($menu['id'] == $menu_id){
                return $menu;
            }
        }
        return [];
    }

    private function initBreadCrumb($menu_id = 0){
        $menu_info = $this->getMenuInfo($menu_id);
        if(empty($menu_info)){
            return;
        }
        !empty($menu_info['parent_id']) && $this->initBreadCrumb($menu_info['parent_id']);
        $this->breadcrumb[] = $menu_info;
    }
    public function getBreadCrumb(){
        return $this->breadcrumb;
    }

    public function getBreadCrumbView(){
        $crumbs_view = "<div class='layui-breadcrumb' lay-filter='breadcrumb'>";
        foreach ($this->breadcrumb as $menu_info) {
//            $icon = empty($menu_info['icon']) ? '' : $menu_info['icon'];
            $url = $menu_info['url'] ? "lay-href='".url($menu_info['url'])."'" : '';
            $crumbs_view .= "<a href='javascript:;' {$url}><cite>".$menu_info['name']."</cite></a>";
        }
        $crumbs_view .= "</div>";
        return $crumbs_view;
    }

    public function menuToView($menu_tree = [],$child = 'child',$is_top = true){
        $menu_view = '';
        //遍历菜单列表
        foreach ($menu_tree as $menu_info) {
            if (!empty($menu_info[$child])) {
                $icon = empty($menu_info['icon']) ? '' : $menu_info['icon'];
                if($is_top) {
                    $menu_view .= "<li data-name=\"{$menu_info['name']}\" data-menu_id='" . $menu_info['id'] . "' class=\"layui-nav-item\">
                                    <a href=\"javascript:;\" lay-tips=\"{$menu_info['name']}\" lay-direction=\"2\">
                                        <i class=\"layui-icon {$icon}\"></i>
                                        <cite>{$menu_info['name']}</cite>
                                    </a>
                                    <dl class=\"layui-nav-child\">
                                        " . $this->menuToView($menu_info[$child], $child, false) . "
                                    </dl>
                                </li>";
                }else{
                    $menu_view .= "<dd data-name=\"{$menu_info['name']}\" data-menu_id='" . $menu_info['id'] . "'>
                                    <a href=\"javascript:;\">
                                        <i class=\"layui-icon {$icon}\"></i>
                                        <cite>{$menu_info['name']}</cite>
                                    </a>
                                    <dl class=\"layui-nav-child\">
                                        " . $this->menuToView($menu_info[$child], $child, false) . "
                                    </dl>
                                </dd>";
                }
            } else {
                $icon = empty($menu_info['icon']) ? '' : $menu_info['icon'];
                $url = url($menu_info['url']);
                if($is_top) {
                    $menu_view .= "<li data-name=\"{$menu_info['name']}\" data-menu_id='" . $menu_info['id'] . "' class=\"layui-nav-item\">
                                    <a href=\"javascript:;\" lay-tips=\"{$menu_info['name']}\" lay-direction=\"2\" lay-href=\"{$url}\">
                                        <i class=\"layui-icon {$icon}\"></i>
                                        <cite>{$menu_info['name']}</cite>
                                    </a>
                                </li>";
                }else{
                    $menu_view .= "<dd>
                                        <a lay-href=\"{$url}\">
                                            <i class='layui-icon ".$icon."'></i>
                                            {$menu_info['name']}
                                        </a>
                                    </dd>";
                }
            }
        }
        return $menu_view;
    }

    /**
     * 菜单转Select
     */
    public function menuToSelect($menu_list = [], $level = 0, $name = 'name', $child = 'child')
    {

        $select_view = [];
        $menu_list_count = count($menu_list);

        foreach ($menu_list as $k => $info) {

            empty($k) && ++$level;

            $tmp_str = str_repeat("&nbsp;", $level * 6) . "├";

            $info[$name] = $tmp_str . $info[$name] . "&nbsp;";

            array_push($select_view, $info);

            if (!array_key_exists($child, $info)) {

                $k != $menu_list_count - DATA_NORMAL ? : $level > DATA_NORMAL && --$level;

            } else {

                $tmp_ary = $info[$child];

                unset($info[$child]);

                $view_arr = $this->menuToSelect($tmp_ary, $level, $name, $child);
                $select_view = array_merge($select_view,$view_arr);
            }
        }

        return $select_view;
    }
}